<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>正则</title>
</head>

<body>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        div {
            width: 300px;
            height: 200px;
            border: 10px solid pink;
            margin: 20px auto;
            padding: 20px;
        }
        
        input {
            width: 100%;
            height: 50px;
            font-size: 24px;
            box-sizing: border-box;
            padding-left: 20px;
        }
        
        p {
            width: 100%;
            height: 30px;
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
        }
        
        p>span {
            width: 30%;
            height: 100%;
            display: flex;
            justify-self: center;
            align-items: center;
            background-color: #ccc;
            color: #fff;
            font-size: 24px;
        }
        
        p>span:nth-child(1).active {
            background-color: red;
        }
        
        p>span:nth-child(2).active {
            background-color: orange;
        }
        
        p>span:nth-child(3).active {
            background-color: green;
        }
    </style>
    <!-- 密码强度校验 -->
    <div>
        <input type="text" />
        <p>
            <span>弱</span>
            <span>中</span>
            <span>强</span>
        </p>
    </div>
    <script>
        let ipt = document.querySelector('input');
        let spans = document.querySelectorAll('span');
        let r1 = /\d/
        let r2 = /[a-z]/i
        let r3 = /[@#%&]/
        ipt.addEventListener('input', () => {
            let text = ipt.value;
            console.log(text);
            let level = 0;
            if (r1.test(text)) level++
                if (r2.test(text)) level++
                    if (r3.test(text)) level++
                        for (let i = 0; i < spans.length; i++) {
                            spans[i].classList.remove('active');
                            if (i < level) {
                                spans[i].classList.add('active');
                            }
                        }
        })
    </script>
</body>

</html>